<?php


namespace app\common\model;


use think\facade\Db;
use think\Model;

class ConsignorItemAddMember extends Model
{
    public function add($data){
        $res = $this->save($data);
        return $res;
    }
    public function getItem($where){
        $res = $this->where($where)->find();
        return $res;
    }
    public function getListByWhere($where){
        $res =  Db::table('consignor_item_add_member')
            ->alias('a')
            ->join(['consignor_item'=>'b'],'a.consignor_item_id=b.id')
            ->where($where)
            ->select();
//        dump($res->toArray());exit();
        return $res;
    }

    /**
     * join分页有条件的获取申请的实时货源和招标公告
     * @param $where
     * @param $paging
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getListByWherePage($where,$paging){
        $num = null;
        if (intval($paging['num'])>0){
            $num = $paging['num'];
        }else{
            $num = 10;
        }
        $current_page = $paging['current_page'];
        $count = Db::table('consignor_item_add_member')
            ->alias('a')
            ->join(['consignor_item'=>'b'],'a.consignor_item_id=b.id')
            ->where($where)->count();
        if ($count<1){
            return [];
        }
        $total = ceil($count/$num);
        $res = Db::table('consignor_item_add_member')
            ->alias('a')
            ->join(['consignor_item'=>'b'],'a.consignor_item_id=b.id')
            ->where($where)
            ->order('b.id', 'desc')->limit(($current_page-1)*$num,$num)
            ->select();
        $pageInfo = [
            "current_page"=>$current_page,
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }
    public function getListByPage($where,$paging){
//        dump($paging);
//        dump($where);exit();
        $num = null;
        if (intval($paging['num'])>0){
            $num = $paging['num'];
        }else{
            $num = 10;
        }
        $current_page = $paging['current_page'];
        $count = $this->where($where)->count();
        if ($count<1){
            return [];
        }
        $total = ceil($count/$num);
        $res = $this->where($where)->order('id', 'desc')->limit(($current_page-1)*$num,$num)->select();
        $pageInfo = [
            "current_page"=>$current_page,
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }
    public function removeItem($id){
        $where = [
            'consignor_item_id'=>$id
        ];
        $result = $this->where($where)->delete();
        return $result;
    }


}